////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.workplaces.display {
	import flash.events.Event;
	import flash.text.TextField;
	
	import kiwi.components.DragObject;
	import kiwi.components.IDropTarget;
	import kiwi.display.DisplayObject;
	import kiwi.utils.Validation;
	
	import plantool.display.skins.EmptySkin;
	import plantool.helpers.TextFieldHelper;

	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class WorkplaceRow extends WorkplaceDisplayObject implements IWorkplaceRow, IDropTarget {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		[Event(name="change", type="flash.events.Event")]
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _tf:TextField			= null;
		private var _hotspot:DragObject		= null;
		private var _height:Number			= 21;
		private var _days:Vector.<IWorkplaceDay> = new Vector.<IWorkplaceDay>();
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public override function get height():Number {
			return _height;
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function WorkplaceRow() {
			super();
			
			//create textfield
			_tf			= TextFieldHelper.createInfoTf();
			_tf.width	= 135;
			_tf.x		= 5;
			_tf.y		= (20 - _tf.height) / 2;
			addChild(_tf);
			
			//create hotspot
			_hotspot			= new DragObject();
			_hotspot.skin		= new EmptySkin();
			_hotspot.width		= 135;
			_hotspot.height		= 20;
			_hotspot.dragItem	= this;
			addChild(_hotspot);
			
			//invalidate
			invalidate();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		protected override function init():void {
			//super init
			super.init();
		}
		
		private function onChange_day(e:Event):void {
			//get height
			var nHeight:Number = 0;
			for (var i:uint = 0; i < _days.length; i++) {
				if (_days[i].getCalculatedHeight() > nHeight) nHeight = _days[i].getCalculatedHeight();
			}
			for (i = 0; i < _days.length; i++) {
				_days[i].height = nHeight;
			}
			
			//set new height
			_height = nHeight + 1;
			
			//invalidate
			draw();
		}
		
		public function addDay(day:IWorkplaceDay):void {
			//position day
			var daypos:uint				= 0;
			var list:Vector.<Date>		= dateApi.getVisibleDates();
			for (var i:uint = 0; i < list.length; i++) {
				if (list[i].valueOf() == day.date.valueOf()) {
					daypos = i;
					break;
				}
			}
			
			(day as DisplayObject).x 	= 145 + (daypos * (day as DisplayObject).width);
			(day as DisplayObject).addEventListener(Event.CHANGE		, onChange_day);
			
			//add day
			_days.push(addChild((day as DisplayObject)));
		}
		
		public function dragObjectOut(target:DragObject):void {
			_tf.textColor = 0xFFFFFF;
		}
		
		public function dragObjectOver(target:DragObject):void {
			_tf.textColor = 0xFF6600;
		}
		
		public override function draw():void {
			//set text
			if (Validation.isSet(workplace)) {
				_tf.text 	= workplace.name;
			}
			
			//draw black background
			graphics.clear();
			graphics.beginFill(0);
			graphics.drawRect(0, 0, 145, _height);
			graphics.endFill();
			
			//draw background behind all days
//			graphics.beginFill(0xCCCCCC);
//			graphics.drawRect(145, 0, width - 145, _height);
//			graphics.endFill();
			
			graphics.beginFill(0);
			graphics.drawRect(145, _height -1 , width - 145, 1);
			graphics.endFill();
			
			//dispatch change event to update height
			dispatchEvent(new Event(Event.CHANGE));
		}
	}
}